package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ExchangeGift;
import cn.wolfcode.crm.domain.Member;
import cn.wolfcode.crm.query.ExchangeGiftQueryObject;
import cn.wolfcode.crm.service.IExchangeGiftService;
import cn.wolfcode.crm.service.IMemberService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author mist
 */
@Controller
@RequestMapping("exchangeGift")
public class ExchangeGiftController {
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IExchangeGiftService exService;

    @RequiresPermissions("exchangeGift:index")
    @PermissionName("积分兑换首页")
    @RequestMapping("")
    public String index() {
        return "exchangeGift/list";
    }

    @RequiresPermissions("exchangeGift:item")
    @PermissionName("积分兑换明细")
    @RequestMapping("item")
    public String item() {
        return "exchangeGift/item";
    }

    @RequiresPermissions("exchangeGift:list")
    @PermissionName("兑换列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ExchangeGiftQueryObject qo) {
        return exService.query(qo);
    }

    @RequestMapping("queryByCardno")
    @ResponseBody
    public Member queryByCardno(Long cardno) {
        return memberService.queryByCardno(cardno);
    }

    @RequestMapping("pointsChange")
    @ResponseBody
    public JsonResult pointsChange(Long cardno, Long totalCount, Long giftId) {
        JsonResult result = new JsonResult();
        try {
            exService.change(cardno, totalCount, giftId);
        } catch (LogicException e) {
            e.printStackTrace();
            result.markMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("兑换失败");
        }
        return result;
    }


    //导出列表
    @RequestMapping("export")
    public void export(HttpServletResponse response) {

        //设置文件名
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = sdf.format(new Date()) + ".xls";

        response.setContentType("application/x-excel");
        response.setCharacterEncoding("UTF-8");
        //excel文件名
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        List<ExchangeGift> list = exService.list();

        try {
            //创建工作簿
            WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());

            //创建页签
            WritableSheet sheet = workbook.createSheet("兑换明细", 0);
            //创建单元格
            String[] header = {"会员卡号", "会员姓名", "礼品名称", "兑换数量", "消费积分", "操作人员", "兑换时间"};
            Label label = null;
            //迭代表头
            for (int i = 0; i < header.length; i++) {
                label = new Label(i, 0, header[i]);
                sheet.addCell(label);
            }

            //迭代数据
            for (int j = 1; j < list.size() + 1; j++) {
                label = new Label(0, j, String.valueOf(list.get(j - 1).getMemberinfoCardno()));
                //添加到页签中
                sheet.addCell(label);
                label = new Label(1, j, list.get(j - 1).getMemberinfoName());
                sheet.addCell(label);
                label = new Label(2, j, list.get(j - 1).getGiftName());
                sheet.addCell(label);
                label = new Label(3, j, String.valueOf(list.get(j - 1).getNumber()));
                sheet.addCell(label);
                label = new Label(4, j, String.valueOf(list.get(j - 1).getGiftIntegral()));
                sheet.addCell(label);
                label = new Label(5, j, String.valueOf(list.get(j - 1).getInputuserUsername()));
                sheet.addCell(label);
                sdf = new SimpleDateFormat("yyyy年MM月dd日");
                label = new Label(6, j, sdf.format(list.get(j - 1).getExchangedate()));
                sheet.addCell(label);
            }

            //写出数据
            workbook.write();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
